package com.vvlhw.pptmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.vvlhw.pptmall.product.entity.CategoryEntity;
import com.vvlhw.pptmall.product.service.CategoryService;
import com.vvlhw.common.utils.PageUtils;
import com.vvlhw.common.utils.R;



/**
 * 商品一级分类表
 *
 * @author lianghanwei
 * @email 15362941982@163.com
 * @date 2020-06-14 16:11:50
 */
@RestController
@RequestMapping("product/category")
@Api(tags = "分类操作")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @ApiOperation("查询所有分类信息")
    @GetMapping("/list")
//    @RequestParam Map<String, Object> params
    //        PageUtils page = categoryService.queryPage(params);
    public R list(){
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        List<CategoryEntity> list = categoryService.list(queryWrapper);
        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @ApiOperation("查询某个分类接口")
    @GetMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@ApiParam("写个3，下面Execute，观察")@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 【商品维护】新增了商品
     * 【分类维护】这边对应分类的数量加 1
     * 需要catId
     */
    @PostMapping("/update/add/{catId}")
    public R updateAdd(@PathVariable("catId") Long catId ){
        categoryService.updateAddGood(catId);
        return R.ok();
    }

    /**
     * 【商品维护】删除了商品，可能单删、可能批量删
     * 【分类维护】这边对应分类的数量减 1
     * 需要catId，可能复数
     *
     */
    @Transactional
    @PostMapping("/update/delete")
    public R updateDelete(@RequestBody Long[] catIds ){
        categoryService.updateDeleteGood(catIds);
        return R.ok();
    }

    /**
     * 批量修改
     */
    @PostMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
